<?php
namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Session;

class News extends Controller
{
    public function _initialize()
    {
        if(!Session::has('Adminname')){
            $this->error('请登录!','../admin');
        }
        if(Session::get('Adminpower')!=1){
            $this->error('您没有访问权限!');
        }
    }
    public function index()
    {
    	$list = Db::name('news')->order('id','asc')->paginate(10);
    	$page = $list->render();
    	$this->assign('list', $list);
		$this->assign('page', $page);
        return $this->fetch();
    }

    //新增新闻
    public function add()
    {
        $param = request()->param();
        $file = request()->file('backgroundimage');
        if($file){
            $info = $file->validate(['ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                $param['img'] = DS.'public'.DS.'uploads'.DS.$info->getSaveName();
            }else{
                // 上传失败获取错误信息
                return json(['message'=>$file->getError()]);
            }
        }
        $news = model('News');
        $data = $news->addnews($param);
        if($data){
            $this->success('创建成功!');
            // return json(['message'=>'创建成功!']);
        }   
        return json(['message'=>$news->getError()],400);
    }

    //更新页面
    public function updatedetail()
    {
        $news = model('News');
        $param = request()->get('id');
        $data = $news::get($param);
        $this->assign('list',$data);
        return $this->fetch('detail/news');
    }

   	//删除新闻
    public function delete()
    {
    	$news = model('News');
    	$param = request()->get('id');
    	$data = $news -> deletenews($param);
    	if($data){
    		return json(['message'=>'删除成功!']);
    	}
    	return json(['message'=>$news->getError()]);
    }

    //更新新闻
    public function update()
    {
    	$param = request()->param();
        $file = request()->file('backgroundimage');
        $info = $file->validate(['ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads');
        if($info){
            $param['img'] = DS.'public'.DS.'uploads'.DS.$info->getSaveName();
        }else{
            // 上传失败获取错误信息
            return json(['message'=>$file->getError()]);
        }
        $news = model('News');
    	$data = $news -> updatenews($param);
        if($data){
            $this->success('更新成功!');
            // return json(['message'=>'更新成功!']);
        }
    	return json(['message'=>$news->getError()]);
    }

    //取消发布
    public function cancel()
    {
    	$news = model('News');
    	$param = request()->get('id');
    	$data=$news->cancelnews($param);
    	if($data){
    		return json(['message'=>'取消发布成功!']);
    	}
    	return json(['message'=>$news->getError()]);
    }

    //发布
    public function fabu()
    {
    	$news = model('News');
    	$param = request()->get('id');
    	$data=$news->fabunews($param);
    	if($data){
    		return json(['message'=>'发布成功!']);
    	}
    	return json(['message'=>$news->getError()]);
    }
}
